package cn.iocoder.yudao.module.system.controller.admin.zkdatanew;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.zkdatanew.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.zkdatanew.ZkdataNewDO;
import cn.iocoder.yudao.module.system.service.zkdatanew.ZkdataNewService;

@Tag(name = "管理后台 - 新数据纪录")
@RestController
@RequestMapping("/system/zkdata-new")
@Validated
public class ZkdataNewController {

    @Resource
    private ZkdataNewService zkdataNewService;

    @PostMapping("/create")
    @Operation(summary = "创建新数据纪录")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new:create')")
    public CommonResult<Long> createZkdataNew(@Valid @RequestBody ZkdataNewSaveReqVO createReqVO) {
        return success(zkdataNewService.createZkdataNew(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新新数据纪录")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new:update')")
    public CommonResult<Boolean> updateZkdataNew(@Valid @RequestBody ZkdataNewSaveReqVO updateReqVO) {
        zkdataNewService.updateZkdataNew(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除新数据纪录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:zkdata-new:delete')")
    public CommonResult<Boolean> deleteZkdataNew(@RequestParam("id") Long id) {
        zkdataNewService.deleteZkdataNew(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得新数据纪录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new:query')")
    public CommonResult<ZkdataNewRespVO> getZkdataNew(@RequestParam("id") Long id) {
        ZkdataNewDO zkdataNew = zkdataNewService.getZkdataNew(id);
        return success(BeanUtils.toBean(zkdataNew, ZkdataNewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得新数据纪录分页")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new:query')")
    public CommonResult<PageResult<ZkdataNewRespVO>> getZkdataNewPage(@Valid ZkdataNewPageReqVO pageReqVO) {
        PageResult<ZkdataNewDO> pageResult = zkdataNewService.getZkdataNewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZkdataNewRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出新数据纪录 Excel")
    @PreAuthorize("@ss.hasPermission('system:zkdata-new:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZkdataNewExcel(@Valid ZkdataNewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZkdataNewDO> list = zkdataNewService.getZkdataNewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "新数据纪录.xls", "数据", ZkdataNewRespVO.class,
                        BeanUtils.toBean(list, ZkdataNewRespVO.class));
    }

}